package serveur;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import message.Message;
import message.MessageChooseFolder;
import message.MessageCloseService;
import message.MessageCreateFolder;
import message.MessageDeleteFolder;
import message.MessageGetFile;
import message.MessageGetFileFullInFolder;
import message.MessageGetFileInFolder;
import message.MessageOpenService;
import message.MessagePutFile;

public class AdminServerDelegate implements Runnable{

	private MainServer mainServer;
	private Socket mSocket;
	private ObjectInputStream mOis;
	private ObjectOutputStream mOus;
	private Logger logger = Logger.getLogger(MainServer.class.getName());
	//private static final Logger logger = Logger.getLogger(MainServer.class.getName());
	
	public AdminServerDelegate(Socket p_sock, MainServer p_mainServer) {
		this.mSocket = p_sock;
		this.mainServer = p_mainServer;
		
		try {
			this.mOis = new ObjectInputStream(mSocket.getInputStream());
			this.mOus = new ObjectOutputStream(mSocket.getOutputStream());
			logger.log(Level.INFO,"Contructeur AdminServerDelegate");
		} catch (IOException ex) {
			logger.log(Level.SEVERE,"Unexpected error",ex);
		}
		
	}


	public void run() {
//		try {
//			socket.close();
//		}
//		catch (Exception e) {	}
	}

	
	public void runAdmin() {
		String reponse = "TPFTP Admin";
		Message reception;
		int messageID = 0;
		Boolean fini = false;
		Boolean authOK = false;	// en pr�vision pour enregistrer l'etat de l'authentification de l'utilisateur
		
		try {
			reception = (Message) mOis.readObject();
			messageID = reception.getMessageId();
			// tri du message en fonction de son ID
			switch (messageID) {
			// MessageOpenService
			case Message.OpenServiceId:
				MessageOpenService messageOpenService = (MessageOpenService) reception;
				messageOpenService.execute();					
				reponse ="Passage dans le case MessageOpenService";
				break;
			default:
				break;
			}
			mOus.writeUTF("Admin : " + reponse);
			}
		catch (ClassNotFoundException ex) {
			logger.log(Level.SEVERE,"Unexpected error",ex);
		}

		catch (IOException ex) {
			logger.log(Level.SEVERE,"Unexpected error",ex);
		}

	}
	
}
